package com.lujianfei.powersearch

import android.content.Intent
import android.os.Bundle
import com.lujianfei.powersearch.base.BaseActivity
import com.lujianfei.powersearch.databinding.SplashActivityBinding
import pub.devrel.easypermissions.EasyPermissions

class SplashActivity: BaseActivity<SplashActivityBinding>(),EasyPermissions.PermissionCallbacks {

    companion object {
        const val PERMISSION_NEED = android.Manifest.permission.WRITE_EXTERNAL_STORAGE
        const val REQUEST_CODE = 100
    }

    override fun resourceId() = R.layout.splash_activity

    override fun initView() {
        if (EasyPermissions.hasPermissions(this,PERMISSION_NEED)) {
            goMain()
        } else {
            EasyPermissions.requestPermissions(this,"需要使用存储权限", REQUEST_CODE, PERMISSION_NEED)
        }
    }

    override fun initData() {
    }

    override fun initEvent() {
    }

    private fun goMain() {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this)
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
        when (requestCode) {
            REQUEST_CODE -> {
                goMain()
            }
        }
    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        finish()
    }
}